package com.kynnnn.cloud.springboot_quartz.vo;

import com.kynnnn.cloud.springboot_quartz.constant.ResultCode;
import lombok.Data;

import java.io.Serializable;

/**
 * <h1>统一返回</h1>
 *
 * @author Zhudiwei
 * @date 2021/10/13
 **/
@Data
public class CommonResponse<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    private Long timestamp;

    /**
     * <h2>成功</h2>
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResponse<T> success(T data) {

        CommonResponse<T> response = new CommonResponse<>();
        response.setSuccess(true);
        response.setCode(ResultCode.SUCCESS.getCode());
        response.setMessage(ResultCode.SUCCESS.getMessage());
        response.setData(data);
        response.setTimestamp(System.currentTimeMillis());

        return response;

    }

    /**
     * <h2>成功</h2>
     *
     * @param <T>
     * @return
     */
    public static <T> CommonResponse<T> success() {

        CommonResponse<T> response = new CommonResponse<>();
        response.setSuccess(true);
        response.setCode(ResultCode.SUCCESS.getCode());
        response.setMessage(ResultCode.SUCCESS.getMessage());
        response.setTimestamp(System.currentTimeMillis());

        return response;

    }

    /**
     * <h2>失败</h2>
     *
     * @param resultCode
     * @return
     */
    public static CommonResponse<Object> fail(ResultCode resultCode) {

        CommonResponse<Object> response = new CommonResponse<>();
        response.setSuccess(false);
        response.setCode(resultCode.getCode());
        response.setMessage(resultCode.getMessage());
        response.setTimestamp(System.currentTimeMillis());

        return response;

    }

    /**
     * <h2>失败</h2>
     *
     * @param errorMsg
     * @return
     */
    public static CommonResponse<Object> fail(String errorMsg) {

        CommonResponse<Object> response = new CommonResponse<>();
        response.setSuccess(false);
        response.setCode(ResultCode.FAIL.getCode());
        response.setMessage(errorMsg);
        response.setTimestamp(System.currentTimeMillis());

        return response;

    }

    /**
     * <h2>失败</h2>
     *
     * @param errorMsg
     * @return
     */
    public static CommonResponse<Object> fail(Integer errorCode, String errorMsg) {

        CommonResponse<Object> response = new CommonResponse<>();
        response.setSuccess(false);
        response.setCode(errorCode);
        response.setMessage(errorMsg);
        response.setTimestamp(System.currentTimeMillis());

        return response;

    }

}
